package com.openai.chatgpt.controller;

import com.openai.chatgpt.dto.ChatBotRequest;
import com.openai.chatgpt.dto.ChatBotResponse;
import com.openai.chatgpt.dto.Message;
import com.openai.chatgpt.properties.Properties;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author mhmdz
 * Created By Zeeshan on 12-11-2023
 * @project chatgtp
 */


@RestController
@RequiredArgsConstructor
public class ChatBotController {

    private final RestTemplate restTemplate;
    private final Properties properties;


    @PostMapping("/chat")
    public ChatBotResponse chat(@RequestParam String content) {

        ChatBotRequest request = new ChatBotRequest(properties.getModel(),
                List.of(new Message("user", content)),
                properties.getMaxCompletions(),
                properties.getTemperature(),
                properties.getMaxTokens());

        return restTemplate.postForObject(properties.getApiUrl(), request, ChatBotResponse.class);
    }
}
